//
//  MainHomeViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/13.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "MainHomeViewController.h"
#import "PaintingCollectionViewCell.h"
#import "BannerReusableView.h"
#import "TypeCollectionViewCell.h"
#import "UIColor+HexM.h"
#import "PaintingDetailViewController.h"
#import "TypeDetailViewController.h"
#import "PaintingDetailViewController.h"
#import <MJRefresh/MJRefresh.h>


@interface MainHomeViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate>
{
    NSMutableArray *bannerArray;
    NSInteger number;
}
//@property (nonatomic, strong) NSMutableArray *bannerArray;
@end

@implementation MainHomeViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"商店" image:[[UIImage imageNamed:@"tabbar_faxian_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_faxian_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"原生瀑布流";
    number = 8;
    [self initCollectionViewUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.4 animations:^{
            [self initData];
            
            [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//            [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil]];
        }];
    });
}
- (void)initData {
    NSArray *array = @[@"http://www.wohoney.com/images/case_img_74.png", @"http://www.wohoney.com/images/case_img_72.png", @"http://www.wohoney.com/images/case_img_71.png", @"http://www.wohoney.com/images/case_img_82.png"];
    bannerArray = [NSMutableArray arrayWithArray:array];
}

- (void)initCollectionViewUI {
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"#B6D0C9"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"PaintingCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PaintingCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TypeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"BannerReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerReusableView"];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadMoreData {
    number = 20;
    [self.collectionView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self->number == 20) {
            [self.collectionView.mj_footer endRefreshing];
        }
    });
}

#pragma mark --CollectionDelegate--
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (bannerArray.count > 0) {
            return 1;
        }else {
            return 0;
        }
    }else if (section == 1) {
        return number;
    }else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        TypeCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = (TypeCollectionViewCell *)[[NSBundle mainBundle] loadNibNamed:@"TypeCollectionViewCell" owner:nil options:nil];
//            cell = [[TypeCollectionViewCell alloc] init];
        }
        cell.dataSourceArray = bannerArray;
        return cell;
    }else {
        PaintingCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"PaintingCollectionViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = (PaintingCollectionViewCell *)[[NSBundle mainBundle] loadNibNamed:@"PaintingCollectionViewCell" owner:nil options:nil];
        }
        if (indexPath.row % 2 != 0) {
            cell.imageView.image = [UIImage imageNamed:@"two.jpg"];
            cell.nameLabel.text = @"小僵尸";
        }else {
            cell.imageView.image = [UIImage imageNamed:@"touxiang.jpg"];
            cell.nameLabel.text = @"小女孩";
        }
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(Screen_Width, (Screen_Width - 30) / 2.5 / 2 * 1 + 20);
    }else {
        return CGSizeMake((Screen_Width - 30) / 2, (Screen_Width - 30) / 2 + 10 + 20 + 10 + 20 + 10);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
//        TypeDetailViewController *type = [TypeDetailViewController initTypeDetailViewController:@"分类啊"];
//        [self.navigationController pushViewController:type animated:YES];
        
//        [self.navigationController pushViewController:[PaintingDetailViewController new] animated:YES];
    }
    if (indexPath.section == 1) {
        PaintingDetailViewController *painting = [[PaintingDetailViewController alloc] init];
        if (indexPath.row % 2 != 0) {
            painting.titleName = @"小僵尸";
            painting.image = [UIImage imageNamed:@"two.jpg"];
        }else {
            painting.titleName = @"小女孩";
            painting.image = [UIImage imageNamed:@"touxiang.jpg"];
        }
        [self.navigationController pushViewController:painting animated:YES];
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0 && bannerArray.count > 0) {
            BannerReusableView *bannerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerReusableView" forIndexPath:indexPath];
            if (!bannerView) {
                bannerView = [[BannerReusableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 728 * 426)];
            }
            bannerView.banerArray = bannerArray;
            return bannerView;
        }else {
            return nil;
        }
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        if (bannerArray.count > 0)  {
            return CGSizeMake(Screen_Width, Screen_Width / 728 * 426);
        }
    }
    return CGSizeZero;
}

#pragma mark --CollectionLayout--
//定义整个CollectionViewCell与整个View的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 10, 10);//（上、左、下、右）
}

////定义每个UICollectionView的横向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}

////定义每个UICollectionView的纵向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 5;
//}

@end
